package com.xy.type4;

/**
 * @author 谢阳
 * @version 1.8.0_131
 * @date 2022/6/9 18:56
 * @description 单例模式 --> 懒汉式（线程安全）
 */
public class SingletonTest04 {
    public static void main(String[] args) {
        Singleton instance1 = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance1 == instance2);
    }
}

class Singleton {
    private static Singleton instance;

    private Singleton() {

    }


    /**
     * 加入synchronized 实现线程安全
     * @return
     */
    public static synchronized Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }

        return instance;
    }


}

